package com.zhengxyou.kotlindemo.basicSyntax

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.zhengxyou.kotlindemo.R

/**
 * 开始与概述
 */
class BasciSyntaxActivity : AppCompatActivity() {

    val a = 1
    val b: Int = 2
    val c: Int = 0
    val number = 1_000_000L

    val d: Byte = 1
    //    val i: Int = d
    val i: Int = d.toInt()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var x = 1
        x = 2
        x = 9
        x += 1
        Log.e("TAG", sum(1, 2).toString())
        Log.e("TAG", sum1(3, 2).toString())
        printSum(4, 6)
        println(x)

        stringTemplate()
        length()
        forTmp()
        whileTmp()
        whenTmp()
        rangeTmp()
        //区间迭代
        intervalIteration()
        //lambda 表达式
        lambadaTmp()
        //过滤list
        val positives = listOf(1, 2, 3, 4, -2).filter { x -> x > 0 }
        val positivesD = listOf(1, 2, 3).filter { it > 0 }

        for (p in positives.indices) {//索引
            print(p)
            print(positives[p])
            println()
        }

        for ((index, value) in positives.withIndex()) {//索引加值
            println("the element at $index is $value")
        }

        //字符串内插
        val name = "lao wan"
        println("Name $name")
        val map = mapOf(Pair(1, 2), Pair(2, 3))
        for ((k, v) in map) {
            println("$k->$v")
        }
        println()
        //区间
        for (x in 1..100) {//闭区间包含100
            print(x)
        }
        println()
        for (x in 1 until 100) {//半开区间，不包含100
            print(x)
        }
        println()
        for (x in 1..100 step 2) {//1，3，5.。。
            print(x)
        }
        println()
        for (x in 10 downTo 1) {//10-1
            print(x)
        }
        //判断是否在1到10
        if (x in 1..10) {

        }
        //只读list
        val list = listOf("a", "b", "c")
        //只读map
        val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
        println()
        //访问map
        println(map1["a"])
        val i = map1["a"]

        //标签
        loop@ for (i in 1..100) {
            for (j in 1..100) {
                if (i > 66) break@loop
            }
        }
        xp@ for (i in 1..100) {
            for (j in 1..100) {
                if (i > 66) break@xp
            }
        }
    }

    //函数的默认参数
    fun foo(a: Int = 0, b: String = "") {

    }

    //标签
    fun fooLabel() {
        listOf(1, 2, 3, 4, 5, 6).forEach {
            if (it == 3) return// 非局部直接返回到 foo() 的调用者
            print(it)
        }
        println("this point is unreachable")
    }
}

fun foo1() {
    listOf(1, 2, 3, 4, 5).forEach(fun(value: Int) {
        if (value == 3) return  // 局部返回到匿名函数的调用者，即 forEach 循环
        print(value)
    })
    print(" done with anonymous function")
}

fun fooLabelDefault() {
    listOf(1, 2, 3, 4, 5, 6).forEach {
        if (it == 3) return@forEach// 默认标签,局部返回到该 lambda 表达式的调用者，即 forEach 循环
        print(it)
    }
    println("this point is unreachable")
}

fun fooLabel1() {
    listOf(1, 2, 3, 4, 5, 6).forEach lint@{
        if (it == 3) return@lint// 局部返回到该 lambda 表达式的调用者，即 forEach 循环
        print(it)
    }
    println("this point is unreachable")
}

//标签
fun fooLabel() {
    listOf(1, 2, 3, 4, 5, 6).forEach {
        if (it == 3) return// 非局部直接返回到 foo() 的调用者
        print(it)
    }
    println("this point is unreachable")
}

private fun lambadaTmp() {
    println()
    val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
    fruits.filter { it.startsWith("a") }
        .sortedBy { it }
        .map { it.toUpperCase() }
        .forEach { println(it) }
}

private fun intervalIteration() {
    for (x in 0..5) {
        println(x)
    }
    println("数列迭代")
    //数列迭代
    for (x in 1..10 step 2) {
        print(x)
    }
    println()
    println("向下到0输出")
    for (x in 9 downTo 0 step 3) {
        print(x)
    }
}

fun rangeTmp() {
    val x = 10
    val y = 9
    if (x in 1..y + 1) {
        println("fits in range")
    }

    val list = listOf("a", "b", "c")
    if (-1 !in 0..list.lastIndex) {
        println("-1 is out of range")
    }
    if (list.size !in list.indices) {
        println("list size is out of valid list indices range, too")
    }
}

private fun whenTmp() {
    println(describe(1))
    println(describe("Hello"))
    println(describe(1L))
    println(describe("string"))
    println(describe(123))
    println(describe(3f))
    val x = 3
    when {//可以不提供参数
        x is Int -> "int"
        else -> "Unknown"
    }
}

fun describe(obj: Any): String =
    when (obj) {
        1, 2 -> "one"
        "Hello" -> "Greeting"
        is Long -> "Long"
        !is String -> "Not a String"
        else -> "Unknown"
    }


private fun whileTmp() {
    val items = listOf("apple", "banana", "kiwifruit")
    var index = 0
    while (index < items.size) {
        println("while>item at $index is ${items[index]}")
        index++
    }
}

fun forTmp() {
    val items = listOf("apple", "banana", "kiwifruit")
    for (item in items) {
        println(item)
    }
    for (index in items.indices) {
        println("for>item at $index is ${items[index]}")
    }
}

private fun length() {
    val a = getStringLength(12);
    val b = getStringLength("12");
    if (a == null) {
        Log.e("TAG", "a is null")
    }
    Log.e("TAG", "b length is $b")
}

private fun getStringLength(obj: Any): Int? {
    if (obj is String) {
        return obj.length
    }
    return null
}

fun getStringLength1(obj: Any): Int? {
    if (obj !is String) {
        return null
    }
    return obj.length
}

fun maxOf(a: Int, b: Int): Int {
    return if (a > b)
        a
    else
        b
}

fun maxOf1(a: Int, b: Int) = if (a > b) a else b

/**
 * 字符串模板
 */
private fun stringTemplate() {
    var a = 1
    val s1 = "a is $a"
    println(s1)
    a = 2
    val s2 = "${s1.replace("is", "was")} ,but now is $a"
    println(s2)
}

//注释
private fun sum(a: Int, b: Int): Int {
    return a + b
}

/**
 *
 */
fun sum1(a: Int, b: Int) = a + b

fun printSum(a: Int, b: Int) {
    println("sum of $a and $b is ${a + b}")
}
